package be.rivendale.renderer.mk2.data;

import be.rivendale.material.Color;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.Serializable;

public class Segment implements Serializable {
    private static final int COLOR_COMPONENTS_PER_PIXEL = 3;

    private Rectangle rectangle;
    private int[] data;

    public Segment(Rectangle rectangle) {
        this.rectangle = rectangle;
        data = new int[rectangle.getWidth() * rectangle.getHeight() * COLOR_COMPONENTS_PER_PIXEL];
    }

    public void plot(int x, int y, Color color) {
        int[] colorComponents = color.toArray();
        int pixelPosition = (y * rectangle.getWidth() + x) * COLOR_COMPONENTS_PER_PIXEL;
        System.arraycopy(colorComponents, 0, data, pixelPosition, colorComponents.length);
    }

    public void mark() {
        BufferedImage image = new BufferedImage(rectangle.getWidth(), rectangle.getHeight(), BufferedImage.TYPE_INT_RGB);
        image.getRaster().setPixels(0, 0, image.getWidth(), image.getHeight(), getData());
        Graphics2D graphics = image.createGraphics();
        graphics.setColor(Color.RED.toAwtColor());
        graphics.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
        graphics.dispose();
        image.getRaster().getPixels(0, 0, rectangle.getWidth(), rectangle.getHeight(), getData());
    }

    public int[] getData() {
        return data;
    }

    public Rectangle getRectangle() {
        return rectangle;
    }
}
